package guda.sqladmin.biz.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import guda.sqladmin.biz.ProjectBiz;
import guda.sqladmin.dao.ProjectDOMapper;
import guda.sqladmin.dao.domain.ProjectDO;
import guda.sqladmin.dao.domain.ProjectDOCriteria;
import guda.tools.web.page.BaseQuery;
import guda.tools.web.page.BizResult;

public class ProjectBizImpl implements ProjectBiz{

    private final static Logger logger = LoggerFactory.getLogger(ProjectBizImpl.class);

    @Autowired
    private ProjectDOMapper projectDOMapper;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            ProjectDO projectDO = projectDOMapper.selectByPrimaryKey(id);
            bizResult.data.put("projectDO", projectDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query Project error",e);
        }
        return bizResult;
}

    public BizResult list(BaseQuery baseQuery) {
        BizResult bizResult = new BizResult();
        try {
            ProjectDOCriteria projectDOCriteria = new ProjectDOCriteria();
            projectDOCriteria.setStartRow(baseQuery.getStartRow());
            projectDOCriteria.setPageSize(baseQuery.getPageSize());
            int totalCount = projectDOMapper.countByExample(projectDOCriteria);
            baseQuery.setTotalCount(totalCount);
            List<ProjectDO> projectDOList = projectDOMapper.selectByExample(projectDOCriteria);
            bizResult.data.put("projectDOList", projectDOList);
            bizResult.data.put("query", baseQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view Project list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            projectDOMapper.deleteByPrimaryKey(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete project error", e);
        }
        return bizResult;
    }

    public BizResult create(ProjectDO projectDO) {
        BizResult bizResult = new BizResult();
        try {
            int count = projectDOMapper.insert(projectDO);
            bizResult.data.put("count", count);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create Project error", e);
        }
        return bizResult;
    }

    public BizResult update(ProjectDO projectDO) {
        BizResult bizResult = new BizResult();
        try {
            int count = projectDOMapper.updateByPrimaryKeySelective(projectDO);
            bizResult.data.put("count", count);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update Project error", e);
        }
        return bizResult;
    }

    }
